package com.fast.fastenjoy.base.utils;

public class ThreadTaskUtils implements Runnable {

	private TaskBean taskBean;
	
	private Object[] objects;
	
	private Thread thread;
	
	private ThreadTaskUtils(TaskBean taskBean, Object[] objects){
		this.taskBean = taskBean;
		this.objects = objects;
	}
	
	/**
	 * 获取实例
	 * @param taskBean
	 * @return
	 */
	public static ThreadTaskUtils getInstance(TaskBean taskBean, Object...objects){
		
		return new ThreadTaskUtils(taskBean, objects);
	}
	
	public void startTask(){
		thread = new Thread(this);
		thread.start();
	}
	
	@SuppressWarnings("deprecation")
	public void stopTask(){
		if(thread != null && thread.isAlive()){
			thread.stop();
		}
	}
	
	public void run() {
		if(taskBean != null){
			taskBean.startTask(objects);
		}
	}

	/**
     * 线程暂停 单位毫秒
     * @param millis
     */
    public static void sleep(long millis){
        
        if(millis < 0){
            
            return;
        }
        
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
        }
    }

	public interface TaskBean {
		public void startTask(Object...objects);
	}
	
}
